from django.shortcuts import render,redirect

from django.http import HttpResponse,JsonResponse

from ..models import Types,Goods

from .TypesViews import GetTypesAll
from .IndexViews import uploads

from django.contrib.auth.decorators import permission_required



# 商品列表
@permission_required('myadmin.show_goods',raise_exception = True)
def goodsindex(request):
    # 获取所有的商品数据
    goodslist = Goods.objects.exclude(status=3)

    # 接受参数 
    types = request.GET.get('types',None)
    keywords = request.GET.get('keywords',None)

    # 判断是否有搜索条件
    if types:
        if types == 'all':
            # 多字段的查询
            # select * from users where username like '%aa%' or age like '%aa%' or
            from django.db.models import Q
            goodslist= goodslist.filter(Q(num__contains=keywords)|Q(price__contains=keywords)|Q(id__contains=keywords)|Q(title__contains=keywords))
        else:
            data = {types+'__contains':keywords}
            goodslist = goodslist.filter(**data)

    # 数据分页
    from django.core.paginator import Paginator
    # 实例化分页对象,参数1,数据列表  参数2 每页显示的条数
    p = Paginator(goodslist, 6)
    # 获取当前的页码数
    page = request.GET.get('page',1)
    # 获取当前页的数据
    goodslist = p.page(page)
    # 获取所有的页码列表
    pages = p.page_range

    # 分配数据
    context = {'goodslist':goodslist}
    # 加载模板
    return render(request,'myadmin/goods/index.html',context)

# 商品添加
@permission_required('myadmin.insert_goods',raise_exception = True)
def goodsadd(request):
    # 获取分类数据
    context = {'typelist':GetTypesAll()}
    # 加载魔板
    return render(request,'myadmin/goods/add.html',context)


# 商品执行添加
@permission_required('myadmin.insert_goods',raise_exception = True)
def goodsinsert(request):
    try:
        # 接收数据
        data = request.POST.dict()
        data.pop('csrfmiddlewaretoken')
        # 上传商品图片
        myfile = request.FILES.get('pic',None)
        if not myfile:
            return HttpResponse('<script>alert("必须选择上传的商品图片");location.href="/myadmin/goods/add/"</script>')  
        # 执行文件上传
        data['pic'] = uploads(myfile)
        # 处理选择的分类数据
        data['tid'] = Types.objects.get(id=data['tid'])
        Goods.objects.create(**data)
        # 跳转到列表页
        return HttpResponse('<script>alert("添加成功");location.href="/myadmin/goods/index/"</script>')
    except:
        return HttpResponse('<script>alert("添加失败");location.href="/myadmin/goods/add/"</script>')

# 删除 逻辑删除 修改为3
@permission_required('myadmin.del_goods',raise_exception = True)
def goodsdelete(request,uid):
    # 获取用户对象
    ob = Goods.objects.get(id=uid)
    # 修改状态
    ob.status = 3
    # 执行
    ob.save()
    # 跳转到列表页
    return HttpResponse('<script>alert("删除成功");location.href="/myadmin/goods/index/"</script>')


# 修改获取数据
@permission_required('myadmin.edit_goods',raise_exception = True)
def goodsedit(request,uid):
    # 获取数据
    ob=Goods.objects.get(id=uid)
    # 分配数据
    context = {'info':ob,'typelist':GetTypesAll()}
    # 加载模板
    return render(request,'myadmin/goods/edit.html',context)

# 执行修改
@permission_required('myadmin.edit_goods',raise_exception = True)
def goodsupdate(request):
    # try:
        # 接受数据
        data = request.POST.dict()
        data.pop('csrfmiddlewaretoken')
        print(data)
        # 获取对象
        ob = Goods.objects.get(id=data['id'])
        ob.title = data['title']
        ob.price = data['price']
        ob.num = data['num']
        # ob.pic = data['pic']
        ob.status = data['status']
        ob.info=data['info']
        from web.settings import BASE_DIR
        import os
        # 判断是否右文件上传
        myfile = request.FILES.get('pic',None)
        if not myfile:
            return HttpResponse('<script>alert("必须选择上传的商品图片");location.href="/myadmin/goods/add/"</script>')  
        # 有新文件上传,删除原有的头像数据,
        os.remove(BASE_DIR+ob.pic)
        ob.pic = uploads(myfile)
        # 执行更新
        ob.save()
        # 跳转到列表页
        return HttpResponse('<script>alert("更新成功");location.href="/myadmin/goods/index/"</script>')
    # except:
    #     return HttpResponse('<script>alert("更新失败");location.href="/myadmin/goods/index/"</script>')



